package com.example.authserver.entity;

import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

/**
 * @author jay
 */
@Data
public class User implements UserDetails, Serializable {
    private static final long serialVersionUID = 3745674432092607042L;

    private Integer id;

    private String username;

    private String password;

    private String nickname;

    private Boolean expired;

    private Boolean locked;

    private String email;

    private String mobile;

    private String sex;

    private String avatar;

    private String createBy;

    private Date createTime;

    private String updateBy;

    private Date updateTime;

    private Boolean delFlag;

    private List<Role> roles;

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<GrantedAuthority> list=new ArrayList<>();
        for(Role role:roles){
            list.add(new SimpleGrantedAuthority("ROLE_"+role.getName()));
        }
        return list;
    }

    @Override
    public boolean isAccountNonExpired() {
        return !expired;
    }

    @Override
    public boolean isAccountNonLocked() {
        return !locked;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}